//
//  LGInspectDetailDoctorController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/11/3.
//

#import "LGInspectDetailDoctorController.h"
#import "LGInspectChartController.h"
#import "LGAnomalyInspectCell.h"
#import "LGInspectDetailPhotoCell.h"
#define viewHeight  (Screen_H-topBarHeight-viewPix(120))
@interface LGInspectDetailDoctorController ()<UITableViewDelegate,UITableViewDataSource>
/**   */
@property (nonatomic , strong)UITableView *tableView;

@end

@implementation LGInspectDetailDoctorController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.tableView];
}

-(void)setPhotoArray:(NSArray *)photoArray{
    _photoArray = photoArray;
    [self.tableView reloadData];
}

-(void)setAnomalyArray:(NSArray *)anomalyArray{
    _anomalyArray = anomalyArray;
    [self.tableView reloadData];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(section == 0 && self.photoArray.count>0){
        return 1;
    }else if (section == 1){
        return self.anomalyArray.count;
    }
    return 0;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section == 0){
        LGInspectDetailPhotoCell *photoCell = [tableView dequeueReusableCellWithIdentifier:@"photoCell" forIndexPath:indexPath];
        photoCell.selectionStyle = UITableViewCellSelectionStyleNone;
        photoCell.photoArray = self.photoArray;
        return photoCell;
        
    }else{
        LGAnomalyInspectCell *anomalyCell = [tableView dequeueReusableCellWithIdentifier:@"anomalyCell" forIndexPath:indexPath];
        anomalyCell.selectionStyle = UITableViewCellSelectionStyleNone;
        if(self.anomalyArray.count == 1){
            anomalyCell.type = @"single";
        }else if (indexPath.row == 0){
            anomalyCell.type = @"top";
        }else if (indexPath.row == self.anomalyArray.count-1){
            anomalyCell.type = @"bottom";
        }else{
            anomalyCell.type = @"middle";
        }
        anomalyCell.dataDic = self.anomalyArray[indexPath.row];
        return anomalyCell;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section == 1){
        LGInspectChartController *controller = [[LGInspectChartController alloc]init];
        controller.code = LGNSString(self.anomalyArray[indexPath.row][@"item_code"]);
        controller.patientNum = self.patientNum;
        controller.patientid = self.patientid;
        [self.navigationController pushViewController:controller animated:YES];
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return (section == 1 && self.anomalyArray.count>0)?viewPix(40):0.01;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    CGFloat height = (section == 1 && self.anomalyArray.count>0)?viewPix(40):0.01;
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, height)];
    headerView.backgroundColor = [UIColor clearColor];
    if(section == 1 && self.anomalyArray.count>0){
        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(viewPix(16), viewPix(20), 4, viewPix(14))];
        lineView.backgroundColor = [UIColor colorWithString:@"#77D4C6"];
        lineView.cornerRidus = 2;
        [headerView addSubview:lineView];
        UILabel *titleLabel = [UILabel lableWithText:@"异常指标" colorString:@"#333333" textFont:LGFontWeight(14, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
        [headerView addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(lineView.mas_right).offset(viewPix(12));
            make.centerY.equalTo(lineView);
        }];
    }
    return headerView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, 0.01)];
}

-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, viewHeight) style:UITableViewStyleGrouped];
        [_tableView registerClass:[LGInspectDetailPhotoCell class] forCellReuseIdentifier:@"photoCell"];
        [_tableView registerClass:[LGAnomalyInspectCell class] forCellReuseIdentifier:@"anomalyCell"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(5))];
    }
    return _tableView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
